﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _7_Reverse
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32 i = 123;

            int result = Reverse(i);

            Console.WriteLine(result);

            Console.ReadLine();
        }

        private static int Reverse(int x)
        {
            int result = 0;

            while (x != 0)
            {
                int r = result * 10;

                if (r / 10 != result)
                {
                    result = 0;
                    break;
                }

                int temp = x % 10;

                x = (x - temp) / 10;

                result = r + temp;
            }

            return result;
        }

        private static int Java(int x)
        {
            int result = 0;

            while (x != 0)
            {
                int tail = x % 10;
                int newResult = result * 10 + tail;
                if ((newResult - tail) / 10 != result)
                { return 0; }
                result = newResult;
                x = x / 10;
            }

            return result;
        }
    }
}
